﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Traders.Views;
using Traders.Models;
using System.Threading;

namespace Traders.Controllers
{
    public class TradersController
    {
        private TradersView _balanceView = null;
        private TradersView _openTradesView = null;

        private TradersModel _balanceModel;
        private TradersModel _openTradesModel;

        private TradersWindow _window;
        public TradersWindow Window { get { return _window; } }

        private AccountManager AccountManager { get; set; }
        
        public TradersController(TradersWindow window) 
        {
            _window = window;
            this.AccountManager = new AccountManager();
            Window.ShowProgressFormWhileBackgroundWorkerRuns(this.AccountManager.ConnectAccounts);
            _balanceModel = new BalanceModel(AccountManager);
            _openTradesModel = new OpenTradesModel(AccountManager);
        }

        public void setBalanceView(TradersView BalanceView)
        {
            _balanceView = BalanceView;
            _balanceView.DataTable = _balanceModel.DataTable;
            _balanceView.EventHandler += _balanceModel.HandleEvent;
            _balanceModel.EventHandler += _balanceView.HandleEvent;

            // Nasłuchujemy w modelu i widoku na połączenia z kontami
            AccountManager.EventHandler += _balanceModel.HandleEvent;
            AccountManager.EventHandler += _balanceView.HandleEvent;
        }
        
        public void setOpenTradesView(TradersView OpenTradesView)
        {
            _openTradesView = OpenTradesView;
            _openTradesView.DataTable = _openTradesModel.DataTable;

            // Podpinamy wzajemne eventy
            _openTradesModel.EventHandler += _openTradesView.HandleEvent;
            _openTradesView.EventHandler += _openTradesModel.HandleEvent;

            AccountManager.EventHandler += _openTradesModel.HandleEvent;
        }

        public void doTicks(int miliseconds)
        {
            _balanceModel.doTicks(miliseconds);
            _openTradesModel.doTicks(miliseconds);
        }

    }
}
